home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / comm / tcp / TcpSpeed.lha / oldsrc / tcpspeed.c
Encoding:
C/C++ Source or Header  |  1998-05-23  |  2.5 KB  |  127 lines

  1. #ifdef AMIGA
  2. #include <proto/socket.h>
  3. #endif
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13. #ifndef AMIGA
  14. #include <sys/times.h>
  15. #endif
  16.  
  17. /* $$$ comment out the following line if your OS uses old-style
  18.        (BSD 4.2) sockaddr structures
  19.    $$$ */
  20. #define HASSINLEN
  21.  
  22. #define PORTNR 37215
  23. #define BUFLEN 32768
  24. #define BUFNUM 640
  25.  
  26. unsigned char buf[BUFLEN];
  27.  
  28. static void usage(char *pname) {
  29.     fprintf(stderr,"Usage: \"%s s\" / \"%s c hostname\"\n",pname,pname);
  30.     exit(0);
  31. }
  32.  
  33. clock_t getelapsed(void) {
  34. #ifdef AMIGA
  35.     return clock();
  36. #else
  37.     struct tms tms;
  38.     return times(&tms);
  39. #endif
  40. }
  41.  
  42. void main(int argc,char *argv[]) {
  43.     char mode;
  44.     int sock;
  45.     int len,totallen;
  46.     struct sockaddr_in sin;
  47.  
  48.     if((argc<2)||(argc>3))
  49.         usage(argv[0]);
  50.     mode=tolower(argv[1][0]);
  51.     if((mode!='c')&&(mode!='s'))
  52.         usage(argv[0]);
  53.     if(((mode=='c')&&(argc!=3))||((mode=='s')&&(argc!=2)))
  54.         usage(argv[0]);
  55.     if(0>(sock=socket(AF_INET,SOCK_STREAM,0))) {
  56.         fprintf(stderr,"Unable to create socket\n");
  57.         exit(0);
  58.     }
  59.     if(mode=='c') {
  60.         struct hostent *he;
  61.         clock_t starttime,endtime;
  62.  
  63.         if(!(he=gethostbyname(argv[2]))) {
  64.             fprintf(stderr,"Host not found\n");
  65.             exit(0);
  66.         }
  67.         sin.sin_family=AF_INET;
  68. #ifdef HASSINLEN
  69.         sin.sin_len=sizeof(sin),
  70. #endif
  71.         memcpy(&sin.sin_addr,he->h_addr,he->h_length);
  72.         sin.sin_port=htons(PORTNR);
  73.         if(0>connect(sock,(struct sockaddr *)&sin,sizeof(sin))) {
  74.             fprintf(stderr,"Unable to connect\n");
  75.             exit(0);
  76.         }
  77.         totallen=0;
  78.         starttime=getelapsed();
  79.         while(0<(len=recv(sock,buf,BUFLEN,0)))
  80.             totallen+=len;
  81.         endtime=getelapsed();
  82. #ifdef AMIGA
  83.         CloseSocket(sock);
  84. #else
  85.         close(sock);
  86. #endif
  87.         printf("%ld bytes read, %d kB/s\n",
  88.          totallen,(((totallen/1024)*CLOCKS_PER_SEC)/(endtime-starttime)));
  89.     } else {
  90.         int sock2,i;
  91.         long on=1;
  92.  
  93.         for(i=0;i<BUFLEN;i++)
  94.             buf[i]=i;
  95. #ifdef SO_REUSEADDR
  96.         setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&on,
  97.          sizeof(on));
  98. #endif
  99.         memset(&sin,0,sizeof(sin));
  100. #ifdef HASSINLEN
  101.         sin.sin_len=sizeof(sin);
  102. #endif
  103.         sin.sin_family=AF_INET;
  104.         sin.sin_addr.s_addr=htonl(INADDR_ANY);
  105.         sin.sin_port=htons(PORTNR);
  106.         if(0>bind(sock,(struct sockaddr *)&sin,sizeof(sin))) {
  107.             fprintf(stderr,"Unable to bind socket\n");
  108.             exit(0);
  109.         }
  110.         listen(sock,5);
  111.         if(0>(sock2=accept(sock,0,0))) {
  112.             fprintf(stderr,"Unable to accept\n");
  113.             exit(0);
  114.         }
  115.         for(i=0;i<BUFNUM;i++)
  116.             if(0>=send(sock2,buf,BUFLEN,0))
  117.                 break;
  118. #ifdef AMIGA
  119.         CloseSocket(sock2);
  120.         CloseSocket(sock);
  121. #else
  122.         close(sock2);
  123.         close(sock);
  124. #endif
  125.     }
  126. }
  127.